home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / c2latex.lha / c++2latex / main.c < prev    next >
Text File  |  1993-08-08  |  8KB  |  361 lines

  1. *
  2.  *  This is a flex input file but should be edited in -*-C-*- mode
  3.  *
  4.  *  C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  5.  *  Copyright (C) 1990 Norbert Kiesel
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 1, or (at your option)
  10.  *  any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  Norbert Kiesel
  22.  *  RWTH Aachen / Institut f. Informatik III
  23.  *  Ahornstr. 55
  24.  *  D-5100 Aachen
  25.  *  West Germany
  26.  *
  27.  *  Phone:  +49 241 80-7266
  28.  *  EUNET:  norbert@rwthi3.uucp
  29.  *  USENET: ...!mcvax!unido!rwthi3!norbert
  30.  *  X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  31.  *
  32.  *  Please contact me for any bugs you find in this code or any
  33.  *  improvements! I'd also be very happy to get feedback where and
  34.  *  how frequently this program is used (just drop a little mail :-).
  35.  *
  36.  *  ---------------------------------------------------------------------------------
  37.  *
  38.  *  C++2LaTeX 2.0: Produce even more prettyprinted LaTeX files from C++ or C sources.
  39.  *
  40.  *  Copyright (C) 1991 Joerg Heitkoetter
  41.  *  Systems Analysis Research Group, University of Dortmund
  42.  *  (heitkoet@gorbi.informatik.uni-dortmund.de).
  43.  *
  44.  */
  45.  
  46. oid
  47. ubstitute (input)
  48. har   *input;
  49.  
  50.     while (*input) {
  51.         switch (*input) {
  52.             case '_':
  53.             case '&':
  54.             case '#':
  55.             case '$':
  56.             case '%':
  57.             case '{':
  58.             case '}':
  59.                 printf ("\\%c", *input);
  60.                 break;
  61.             case '+':
  62.             case '=':
  63.             case '<':
  64.             case '>':
  65.                 printf ("$%c$", *input);
  66.                 break;
  67.             case '*':
  68.                 printf ("$\\ast$");
  69.                 break;
  70.             case '|':
  71.                 printf ("$\\mid$");
  72.                 break;
  73.             case '\\':
  74.                 printf ("$\\backslash$");
  75.                 break;
  76.             case '^':
  77.                 printf ("$\\wedge$");
  78.                 break;
  79.             case '~':
  80.                 printf ("$\\sim$");
  81.                 break;
  82.             default:
  83.                 printf ("%c", *input);
  84.                 break;
  85.         }
  86.         input++;
  87.     }
  88.  
  89.  
  90. oid
  91. ndent (blanks)
  92. har   *blanks;
  93.  
  94.     int     i;
  95.  
  96.     i = 0;
  97.     while (*blanks) {
  98.         if (*blanks == ' ') {
  99.             i++;
  100.         } else {           /* *blanks == '\t' */
  101.             while (++i % tabtotab);
  102.         }
  103.         blanks++;
  104.     }
  105.     printf ("\\hspace*{%d\\indentation}", i);
  106.  
  107.  
  108. include "getopt.h"
  109. include <string.h>
  110. include <fcntl.h>
  111. include <ctype.h>
  112. include <time.h>
  113.  
  114. xtern char *version_string;
  115.  
  116. tatic struct option opts[] =
  117.  
  118.     {"ansi-c", 0, 0, 'a'},
  119.     {"complete-file", 0, 0, 'c'},
  120.     {"font-size", 1, 0, 's'},
  121.     {"indentation", 1, 0, 'i'},
  122.     {"header", 0, 0, 'h'},
  123.     {"piped", 0, 0, 'p'},
  124.     {"alignment", 0, 0, 'n'},      /* turn off comment alignment  -joke */
  125.     {"output", 1, 0, 'o'},
  126.     {"tabstop", 1, 0, 'T'},
  127.     {"brace-tab", 1, 0, 'b'},      /* added new tabtobrace  -joke */
  128.     {"comment-font", 1, 0, 'C'},
  129.     {"string-font", 1, 0, 'S'},
  130.     {"keyword-font", 1, 0, 'K'},
  131.     {"header-font", 1, 0, 'H'},
  132.     {"cpp-font", 1, 0, 'P'},
  133.     {"version", 0, 0, 'V'},
  134.     {0, 0, 0, 0}
  135. ;
  136.  
  137.  
  138. ain (argc, argv)
  139. nt     argc;
  140. har  **argv;
  141.  
  142.     int     c;
  143.     int     index;
  144.     int     i;
  145.     int     has_filename;
  146.     char   *input_name;
  147.     char   *output_name;
  148.     char   *program_name;
  149.     long    now;
  150.     char   *today;
  151.     char   *malloc ();
  152.  
  153.     input_name = "Standard Input";
  154.     output_name = 0;
  155.  
  156.     now = time (0);
  157.     today = ctime (&now);
  158.  
  159.     program_name = strrchr (argv[0], '/');
  160.     if (program_name == NULL) {    /* no pathname */
  161.         program_name = argv[0];
  162.     } else {
  163.         program_name++;
  164.     }
  165.  
  166.  /* simple heuristic: '+' in name means C++ */
  167.     cplusplus_mode = (strchr (program_name, '+') != 0);
  168.  
  169.     if (argc == 1)
  170.         usage (program_name);  /* added exit with usage  -joke */
  171.  
  172.     while ((c = getopt_long (argc, argv,
  173.                  "acpno:s:i:b:hT:C:H:S:K:P:V", opts, &index))
  174.             != EOF) {
  175.         if (c == 0) {           /* Long option */
  176.             c = opts[index].val;
  177.         }
  178.         switch (c) {
  179.             case 'a':
  180.                 cplusplus_mode = 0;
  181.                 break;
  182.             case 'c':
  183.                 complete_file = 1;
  184.                 break;
  185.             case 'o':
  186.                 if (piped) {
  187.                     fprintf (stderr,
  188.                          "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  189.                          program_name);
  190.                     exit (5);
  191.                 }
  192.                 output_name = optarg;
  193.                 break;
  194.             case 'n':
  195.                 aligntoright = 0;
  196.                 break;
  197.             case 's':
  198.                 font_size = optarg;
  199.                 break;
  200.             case 'i':
  201.                 indentation = optarg;
  202.                 break;
  203.             case 'b':
  204.                 tabtobrace = atoi (optarg);
  205.                 break;
  206.             case 'T':
  207.                 tabtotab = atoi (optarg);
  208.                 break;
  209.             case 'p':
  210.                 if (output_name != 0) {
  211.                     fprintf (stderr,
  212.                          "%s: Can't use {-p,+pipe} and {-o,+output} together\n",
  213.                          program_name);
  214.                     exit (5);
  215.                 }
  216.                 piped = 1;
  217.                 break;
  218.             case 'h':
  219.                 header = 1;
  220.                 complete_file = 1;    /* header implies
  221.                              * complete-file */
  222.                 break;
  223.             case 'C':
  224.                 comment_font = optarg;
  225.                 break;
  226.             case 'H':
  227.                 header_font = optarg;
  228.                 break;
  229.             case 'P':
  230.                 cpp_font = optarg;
  231.                 break;
  232.             case 'S':
  233.                 string_font = optarg;
  234.                 break;
  235.             case 'K':
  236.                 keyword_font = optarg;
  237.                 break;
  238.             case 'V':
  239.                 fprintf (stderr, "%s\n", version_string);
  240.                 break;
  241.             default:
  242.                 usage (program_name);
  243.         }
  244.     }
  245.     has_filename = (argc - optind == 1);
  246.     if (has_filename) {           /* last argument is input file name */
  247.         input_name = argv[optind];
  248.         if (freopen (input_name, "r", stdin) == NULL) {
  249.             fprintf (stderr, "%s: Can't open `%s' for reading\n",
  250.                  program_name, input_name);
  251.             exit (2);
  252.         }
  253.     }
  254.     if ((output_name == 0) && !piped) {
  255.         char   *tmp;
  256.         if (has_filename) {
  257.             tmp = strrchr (input_name, '/');
  258.             if (tmp == 0) {    /* plain filename */
  259.                 tmp = input_name;
  260.             } else {
  261.                 tmp++;
  262.             }
  263.         } else {
  264.             tmp = program_name;
  265.         }
  266.         output_name = malloc (strlen (tmp) + 4);
  267.         if (output_name == 0) {
  268.             fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  269.             exit (3);
  270.         }
  271.         strcpy (output_name, tmp);
  272.         strcat (output_name, ".tex");
  273.     }
  274.     if (!piped) {
  275.         if (freopen (output_name, "w", stdout) == NULL) {
  276.             fprintf (stderr, "%s: Can't open `%s' for writing\n",
  277.                  program_name, output_name);
  278.             exit (3);
  279.         }
  280.     }
  281.     printf ("\
  282. %\n\
  283. % This file was automatically produced at %.24s by\n\
  284. % %s", today, program_name);
  285.     for (i = 1; i < argc; i++) {
  286.         printf (" %s", argv[i]);
  287.     }
  288.     if (!has_filename) {
  289.         printf (" (from Standard Input)");
  290.     }
  291.     printf ("\n%%\n");
  292.     if (complete_file) {
  293.         if (header) {
  294.             if (strcmp (font_size, "10") == 0) {
  295.                 printf ("\\documentstyle[fancyheadings]{article}\n");
  296.             } else {
  297.                 printf ("\\documentstyle[%spt,fancyheadings]{article}\n",
  298.                     font_size);
  299.             }
  300.         } else {
  301.             if (strcmp (font_size, "10") == 0) {
  302.                 printf ("\\documentstyle{article}\n");
  303.             } else {
  304.                 printf ("\\documentstyle[%spt]{article}\n", font_size);
  305.             }
  306.         }
  307.         printf ("\\setlength{\\textwidth}{16cm}\n");
  308.         printf ("\\setlength{\\textheight}{23cm}\n");
  309.         printf ("\\setlength{\\hoffset}{-2cm}\n");
  310.         printf ("\\setlength{\\voffset}{-2cm}\n");
  311.         if (header) {
  312.             printf ("\\lhead{\\%s ", header_font);
  313.             substitute (input_name);
  314.             printf ("}");
  315.             printf ("\\rhead{\\rm\\thepage}\n");
  316.             printf ("\\cfoot{}\n");
  317.             printf ("\\addtolength{\\headheight}{14pt}\n");
  318.             printf ("\\pagestyle{fancy}\n");
  319.         }
  320.         printf ("\\begin{document}\n");
  321.     }
  322.     printf ("\\expandafter\\ifx\\csname indentation\\endcsname\\relax%\n");
  323.     printf ("\\newlength{\\indentation}\\fi\n");
  324.     printf ("\\setlength{\\indentation}{%s}\n", indentation);
  325.     printf ("\\begin{flushleft}\n");
  326.     yylex ();
  327.     printf ("\\end{flushleft}\n");
  328.     if (complete_file) {
  329.         printf ("\\end{document}\n");
  330.     }
  331.     exit (0);
  332.  
  333.  
  334. oid
  335. sage (name)
  336. har   *name;
  337.  
  338.     fprintf (stderr, "%s\n", version_string);
  339.     fprintf (stderr, "\
  340. sage: %s [options] file\n\n\
  341. ptions:\n\
  342.     [-a]            [-b]\n\
  343.     [-c]            [-h]\n\
  344.     [-i length]        [-n]\n\
  345.     [-o file]        [-p]\n\
  346.     [-s fontsize]        [-C font]\n\
  347.     [-H font]        [-K font]\n\
  348.     [-P font]        [-S font]\n\
  349.     [-T tabulatorwidth]    [-V]\n\
  350.     \n\
  351.     [+ansi-c]        [+brace-tab indentation]\n\
  352.     [+complete-file]    [+header]\n\
  353.     [+indentation length]    [+no-alignment]\n\
  354.     [+output file]        [+pipe]\n\
  355.     [+font-size size]    [+comment-font font]\n\
  356.     [+keyword-font font]    [+cpp-font font]\n\
  357.     [+header-font font]    [+string-font font]\n\
  358.     [+tabstop width]    [+version]\n", name);
  359.     exit (1);
  360.  
  361.